Ansible Roles

Ansible Roles基本概述

roles不管是Ansible还是saltstack,我在写一键部署的时候,都不可能把所有的步骤全部写入到一个'剧本'文件当中,我们肯定需要把不同的工作模块,拆分开来,解耦,那么说到解耦,我们就需要用到roles官方推荐,因为roles的目录结构层次更加清晰。

例如:我们之前推荐大家写一个base.yml里面写所有基础优化的项目,其实把所有东西摞进去也是很鸡肋的,不如我们把这些功能全部拆分开,谁需要使用,就调用即可。

建议:每个roles最好只使用一个tasks这样方便我们去调用,能够很好的做到解耦。(SOA)

Ansible Roles目录结构

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-0tyrCbCj-1579158225203)(https://www.driverzeng.com/wp-content/uploads/2019/09/15689649055718.jpg)]

production                # inventory file for production servers
staging                   # inventory file for staging environment

group_vars/
   group1.yml             # here we assign variables to particular groups
   group2.yml
host_vars/
   hostname1.yml          # here we assign variables to particular systems
   hostname2.yml

library/                  # if any custom modules, put them here (optional)
module_utils/             # if any custom module_utils to support modules, put them here (optional)
filter_plugins/           # if any custom filter plugins, put them here (optional)

site.yml                  # master playbook
webservers.yml            # playbook for webserver tier
dbservers.yml             # playbook for dbserver tier

roles/
    common/               # this hierarchy represents a "role"
        tasks/            #  动作,所有的task操作,写在这个下面,最后用main统一调用
            main.yml      #  <-- tasks file can include smaller files if warranted
        handlers/         #  触发器的动作都写在这里面
            main.yml      #  <-- handlers file
        templates/        #  使用到变量的文件写在这里面,当使用template模块的时候会自己来调用
            ntp.conf.j2   #  <------- templates end in .j2
        files/            #  使用copy模块照样子把源文件退到远端服务器来这里取文件
            bar.txt       #  <-- files for use with the copy resource
            foo.sh        #  <-- script files for use with the script resource
        vars/             #  当前roles的变量
            main.yml      #  <-- variables associated with this role
        defaults/         #  当前roles的变量,优先级低
            main.yml      #  <-- default lower priority variables for this role
        meta/             #  依赖,执行当前roles之前先执行该目录下面的roles
            main.yml      #  <-- role dependencies
        library/          # roles can also include custom modules
        module_utils/     # roles can also include custom module_utils
        lookup_plugins/   # or other types of plugins, like lookup in this case

    webtier/              # same kind of structure as "common" was above, done for the webtier role
    monitoring/           # ""
    fooapp/               # ""

ansible Galaxy命令

#####创建roles结构目录(不用手动创建),创建的目录在当前目录下,所以最好提前进到roles目录下
[root@m01 roles]# ansible-galaxy init rsync
[root@m01 ~]# cd /etc/ansible/roles/
[root@m01 roles]# tree rsync/
nfs/                #项目名称
├── defaults        #低优先级变量
├── files           #存放文件
├── handlers        #触发器文件
├── meta            #依赖关系文件
├── tasks           #工作任务文件
├── templates       #jinja2模板文件
├── tests           #测试文件
└── vars            #变量文件


#查看帮助
[root@m01 ~]# ansible-galaxy --help

#搜索库里面的项目(galaxy就是一个代码库)
[root@m01 ~]# ansible-galaxy search openvpn

#安装下载库里的项目(下载后会告诉你目录位置)
[root@m01 ~]# ansible-galaxy install kyl191.openvpn

#查看库里面项目的信息
[root@m01 ~]# ansible-galaxy info kostyrevaa.openvpn

Ansible Roles依赖关系

roles允许你再使用roles时自动引入其他的roles。role依赖关系存储在roles目录中meta/main.yml文件中。

例如:推送wordpress并解压,前提条件,必须要安装nginx和php,把服务跑起来,才能运行wordpress的页面,此时我们就可以在wordpress的roles中定义依赖nginx和php的roles

如果编写了meta目录下的main.yml文件,那么Ansible会自动先执行meta目录中main.yml文件中的dependencies文件,如上所示,就会先执行nginx和php的安装。
[root@m01 roles]# vim /etc/ansible/roles/wordpress/meta/main.yml
dependencies:
  - { role: nginx }
  - { role: php }

Ansible Roles实践

Copyright © 高程程 all right reserved,powered by Gitbook修订于: 2021-05-18 21:14:35

results matching ""

    No results matching ""